package thread;

/**
 * stop()不安全
 * suspend()容易死锁
 * sleep(long)睡眠后自动醒
 * wait()等待需要notify()或notifyAll()唤醒
 * 线程和同步
 * 假设一个银行有3个用户要存储五百元钱,这五百元钱那每人分五次存储也就是每人每次只能存储一百,最后要打印出钱的总和也就是1500;
 * 
 * @author Administrator
 * 
 */
class Bank {
	public static int sum = 0;

	public synchronized static void add(int n, String name) {

		sum += n;
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			System.out.println(e.toString());
		}

		System.out.println(name + " sum=" + sum);
	}
}

class customer extends Thread {
	String name;

	public customer(String name) {
		this.name = name;
	}

	public void run() {
		for (int i = 1; i <= 5; i++) {
			System.out.println(Bank.sum);
			Bank.add(100, name);
		}
	}

	public static void main(String args[]) {
		customer c1 = new customer("a");
		customer c2 = new customer("b");
		customer c3 = new customer("c");
		c1.start();
		c2.start();
		c3.start();
	}

}
